#include <lib/holyhttp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "cgi.h"
#include "blog.h"
#include "comment.h"
#include "guest.h"

#define SITE_NAME "我是我自己的"
#define COPYRIGHT "曾凡帆"
#define BOOTSTRAP_CSS "//cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
// #define BOOTSTRAP_CSS "/static/bootstrap.min.css"
#define BOOTSTRAP_JS "//cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"
// #define BOOTSTRAP_JS "/static/bootstrap.min.js"
#define TOASTR_CSS "//cdn.bootcdn.net/ajax/libs/toastr.js/latest/toastr.min.css"
// #define TOASTR_CSS "/static/toastr.min.css"
#define TOASTR_JS "//cdn.bootcdn.net/ajax/libs/toastr.js/latest/toastr.min.js"
// #define TOASTR_JS "/static/toastr.min.js"
#define JQUERY_JS "//cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"
// #define JQUERY_JS "/static/jquery.min.js"

long g_start_time;
char *g_site_name = SITE_NAME;
char *g_copyright = COPYRIGHT;
static char *navbar_html = \
"<nav class=\"navbar navbar-default\">"\
  "<div class=\"container-fluid\">"\
    "<div class=\"navbar-header\">"\
      "<a class=\"navbar-brand\"href=\"/\">"\
        "<img src=\"/static/favicon.ico\"alt=\"HOME\"></a></div>"\
    "<p class=\"navbar-text\"><a class=\"navbar-link\"href=\"/\">"SITE_NAME"</a></p>"\
    "<ul class=\"nav navbar-nav navbar-right\">"\
      "<li id=\"nav_login\"><a href=\"/login\"class=\"navbar-brand\">"\
        "<i class=\"glyphicon glyphicon-log-in\"> </i> 登录</a></li>"\
      "<li id=\"nav_newblog\"><a target=\"_blank\" href=\"/blog/add\"class=\"navbar-brand\">"\
        "<i class=\"glyphicon glyphicon-pencil\"> </i> 写文章</a></li>"\
      "<li id=\"nav_logout\"><a href=\"/logout\"class=\"navbar-brand\">"\
        "<i class=\"glyphicon glyphicon-log-out\"> </i> 登出</a></li>"\
    "</ul>"\
  "</div>"\
"</nav>";
static char *footer_html = \
"<footer class=\"fixed\">"\
  "Copyright &copy; 2018 <a href=\"/\">"COPYRIGHT"</a> | "\
  "<a href=\"https://beian.miit.gov.cn/\" target=\"_blank\"> "\
    "闽ICP备16020684号-1</a> | "\
  "<a href=\"/contact\" target=\"_blank\">联系我们</a>　"\
"</footer>";
static char *statistics_script = "\
<script>\
var _hmt = _hmt || [];\
(function() {\
  var hm = document.createElement('script');\
  hm.src = 'https://hm.baidu.com/hm.js?40cfbd7fb68ef0fb90a37e4f8251496b';\
  var s = document.getElementsByTagName('script')[0];\
  s.parentNode.insertBefore(hm, s);\
})();\
</script>\
";

static void response_delay(holyreq_t *req)
{
    char *delay = req->get_arg(req, "delay");

    if (delay) {
        req->send_html(req, delay);
    } else {
        req->send_html(req, "arg <delay> is null!");
    }

    req->free(req);
    free(req);
}

static void cgi_delay(holyreq_t *req)
{
    char *delay = req->get_arg(req, "delay");
    holyreq_t *cpy;

    if (!delay) {
        req->send_html(req, "arg <delay> is needed");
        return;
    }

    cpy = req->clone(req);
    if (!cpy) {
        req->send_html(req, "out of memory!");
        return;
    }

    req->incomplete = 1;

    holy_set_timeout(atoi(delay), response_delay, cpy);
}

static void cgi_chunked(holyreq_t *req)
{
    char * fail = "{\r\n" \
    "    \"message-count\": \"1\",\r\n" \
    "    \"messages\": [{\r\n" \
    "        \"to\": \"123\",\r\n" \
    "        \"status\": \"29\",\r\n" \
    "        \"error-text\": \"Non White-listed Destination - rejected\"\r\n" \
    "    }]\r\n" \
    "}";
    char *ok = "{\r\n"\
    "    \"message-count\": \"1\",\r\n"\
    "    \"messages\": [{\r\n"\
    "        \"to\": \"861802697593211\",\r\n"\
    "        \"message-id\": \"0E0000007917F74D\",\r\n"\
    "        \"status\": \"0\",\r\n"\
    "        \"remaining-balance\": \"0.67460000\",\r\n"\
    "        \"message-price\": \"0.02820000\",\r\n"\
    "        \"network\": \"46003\"\r\n"\
    "    }]\r\n"\
    "}";
    char *data = ok;
    req->response(req, 200, data, strlen(data), "text/json", 0, 1, NULL, NULL, NULL);
    data = fail;
    req->response(req, 200, data, strlen(data), "text/json", 0, 1, NULL, NULL, NULL);
    req->response(req, 200, "", 0, "text/json", 0, 1, NULL, NULL, NULL);
}

void init_cgi(void)
{
    g_start_time = time(NULL);

    init_blog();
    init_comment();
    init_guest();

#if 0
    {
        db_value_t values[BLOG_COL_NUM] = {0};
        values[BLOG_COL_TITLE].s = g_test_blog_title;
        values[BLOG_COL_CONTENT].s = g_test_blog_content;
        values[BLOG_COL_CREATED_TIME].s = get_datetime_str();
        values[BLOG_COL_LAST_MODIFIED].s = get_datetime_str();
        values[BLOG_COL_ACTIVE].i = 1;
        blogs.add_or_replace(&blogs, values);
    }
#endif

#if 1
    holyhttp_set_common_render_args("\x01",
        "g.start_time=%ld\x01"
        "g.site_name=%s\x01"
        "g.copyright=%s\x01"
        "g.navbar=%s\x01"
        "g.footer=%s\x01"
        "g.bootstrap_css=%s\x01"
        "g.bootstrap_js=%s\x01"
        "g.toastr_css=%s\x01"
        "g.toastr_js=%s\x01"
        "g.jquery_js=%s\x01"
        "g.statistics=%s",
        g_start_time,
        g_site_name,
        g_copyright,
        navbar_html,
        footer_html,
        BOOTSTRAP_CSS,
        BOOTSTRAP_JS,
        TOASTR_CSS,
        TOASTR_JS,
        JQUERY_JS,
        statistics_script);

    holyhttp_set_white_route("/", cgi_index);
    holyhttp_set_white_route("is_online", cgi_is_online);
    holyhttp_set_white_route("login", cgi_login);
    holyhttp_set_white_route("blog", cgi_show_blog);
    //holyhttp_set_white_route("comment/add", cgi_add_comment);
    holyhttp_set_route("logout", cgi_logout);
    holyhttp_set_route("blog/add", cgi_add_blog);
    holyhttp_set_route("blog/mdf", cgi_modify_blog);
    holyhttp_set_route("blog/del", cgi_del_blog);
    holyhttp_set_white_route("comment/add", cgi_add_comment);
    holyhttp_set_route("comment/del", cgi_del_comment);
#endif

    // test
    holyhttp_set_white_route("chunked", cgi_chunked);
    holyhttp_set_white_route("delay", cgi_delay);
}

